<?php

require_once __DIR__ . '/../../../../vendor/autoload.php';

use JLPay\Core\OrgConfig;
use JLPay\Services\Trans\Mini\Wx\AuthBindService;
use JLPay\Models\Trans\Mini\Wx\AuthBindRequest;
use JLPay\Models\Trans\Mini\Wx\AuthBindResponse;
use JLPay\Utils\Logger;
use JLPay\Utils\DemoRequestHandler;

/**
 * 交易 - Mini微信授权绑定功能使用示例
 */

// =================== 密钥配置 =====================
// SM2公私钥
$sm2PublicKeyPem = "-----BEGIN PUBLIC KEY-----
MFkwEwYHKoZIzj0CAQYIKoEcz1UBgi0DQgAEJJhFmjeMRp9GwShxtWunv6Wny//m
Dg2pcnu0llDQvIYo5O3R+al39NekciZ0BIrT21Y/QJrXgJIoyBr6STml6g==
-----END PUBLIC KEY-----";

$sm2PrivateKeyPem = "-----BEGIN EC PRIVATE KEY-----
MIGTAgEAMBMGByqGSM49AgEGCCqBHM9VAYItBHkwdwIBAQQgHqOp471SMDsQml2G
2ErpZPLD2X7rbMqa6P1O9Zqlhe+gCgYIKoEcz1UBgi2hRANCAAQkmEWaN4xGn0bB
KHG1a6e/pafL/+YODalye7SWUNC8hijk7dH5qXf016RyJnQEitPbVj9AmteAkijI
GvpJOaXq
-----END EC PRIVATE KEY-----";

// 嘉联支付平台公钥
$jlpayPubKeyPem = "-----BEGIN PUBLIC KEY-----
MIIBMzCB7AYHKoZIzj0CATCB4AIBATAsBgcqhkjOPQEBAiEA/////v//////////
//////////8AAAAA//////////8wRAQg/////v////////////////////8AAAAA
//////////wEICjp+p6dn140TVqeS89lCafzl4n1FauPkt28vUFNlA6TBEEEMsSu
LB8ZgRlfmQRGajnJlI/jC7/yZgvhcVpFiTNMdMe8Nzai9PZ3nFm9zuNraSFT0KmH
fMYqR0AC3zLlITnwoAIhAP////7///////////////9yA99rIcYFK1O79Ak51UEj
AgEBA0IABKZ3VWwH0Aw9xvOWXb+xM2gXyApWEAZLFy8UyIigp3ehOTwPJ693njrg
NavTLdzAtFBqXRreivlwBTIFy8iRgn8=
-----END PUBLIC KEY-----";

// =================== 工具函数 =====================
/**
 * 创建配置对象
 */
function createConfig(): OrgConfig
{
    global $sm2PrivateKeyPem, $sm2PublicKeyPem, $jlpayPubKeyPem;
    
    return OrgConfig::builder()
        ->appId("1vzougr30f9vcw")
        ->url("https://openapi-uat.jlpay.com")
        ->orgPriKey($sm2PrivateKeyPem)
        ->orgPubKey($sm2PublicKeyPem)
        ->jlpayPubKey($jlpayPubKeyPem)
        ->build();
}

/**
 * 构建微信授权绑定请求
 */
function buildAuthBindRequest(): AuthBindRequest
{
    $request = new AuthBindRequest();
    
    // 设置请求参数
    $request->setMchId("849584358120018")
        ->setPayType("wxpay")
        ->setJsapiPath("http://wx.com/jspay/pay/")
        ->setSubAppid("wx1234567890abcdef")
        ->setMchCreateIp("127.0.0.1");
    
    return $request;
}

// =================== 示例函数 =====================
/**
 * 微信授权绑定示例
 */
function authBindDemo(): void
{
    $logger = Logger::getInstance();
    $logger->info("开始执行微信授权绑定示例");
    
    // 创建配置对象和微信授权绑定服务
    $config = createConfig();
    $authBindService = new AuthBindService($config);
    
    // 创建微信授权绑定请求
    $request = buildAuthBindRequest();
    
    // 发送请求 - 使用DemoRequestHandler
    $response = DemoRequestHandler::sendRequestAndHandleResponse(
        $authBindService, 
        $request, 
        "微信授权绑定",
        function($service, $request) {
            return $service->authBind($request);
        }
    );
    
    // 处理响应结果
    if ($response && $response->isSuccess()) {
        $logger->info("响应报文：" . json_encode($response->toArray(), JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES));
    }
    
    $logger->info("微信授权绑定示例执行完毕");
}

// =================== 主程序 =====================
$logger = Logger::getInstance();

$logger->info("=" . str_repeat("=", 50));
$logger->info("===== 微信授权绑定示例 开始 =====");
$logger->info("=" . str_repeat("=", 50));

authBindDemo();

$logger->info("=" . str_repeat("=", 50));
$logger->info("===== 微信授权绑定示例执行完毕 =====");
$logger->info("=" . str_repeat("=", 50)); 